package com.supinfo.suptracking.servlet;

import com.supinfo.suptracking.entity.User;
import com.supinfo.suptracking.service.UserService;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Fabien, Jérémy, Ludovic, Brice
 */

@WebServlet(name = "RegisterServlet", urlPatterns = {"/register"})
public class RegisterServlet extends HttpServlet {
    
    @EJB
    private UserService userService;
        
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.sendRedirect(getServletContext().getContextPath() + "/jsp/register.jsp");		
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        
        User user = new User();
        User userTestEmail = null;
        user.setUserAdmin(false);
        String username = req.getParameter("userName");
        String firstname = req.getParameter("firstName");
        Long phonenumber = Long.parseLong(req.getParameter("phoneNumber"));
        String postaladdress = req.getParameter("postalAddress");
        String lastname = req.getParameter("lastName");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        Long creditcard = Long.parseLong(req.getParameter("creditCard"));
        
        List<User> userInDB = userService.processGetAllUser();
        userTestEmail = userService.processGetUserByMail(email);
        User userTestUsername = userService.processGetUserByUsername(username);
        
        if (userInDB.isEmpty()) {
            user.setUserAdmin(true);
        }
        if (userTestEmail!=null || userTestUsername!=null){
            resp.sendRedirect(req.getServletContext().getContextPath()+"/jsp/register.jsp");
            req.getSession().setAttribute("event", "0");
            req.getSession().setAttribute("message", "Mail or Username  already exist"); 
        }
        else{
            user.setUserName(username);
            user.setFirstName(firstname);
            user.setLastName(lastname);
            user.setCreditCard(creditcard);
            user.setPassword(password);
            user.setPostalAddress(postaladdress);
            user.setPhoneNumber(phonenumber);
            user.setEmail(email);
            user.setActualLatitude("0");
            user.setActualLongitude("0");

            System.out.println("Firstname : " + user.getFirstName());
            System.out.println("Lastname : " + user.getLastName());
            System.out.println("Email : " + user.getEmail());
            System.out.println("Postal Address : " + user.getPostalAddress());
            System.out.println("Latitude : " + user.getActualLatitude());
            System.out.println("Longitude : " + user.getActualLongitude());
            System.out.println("Car list : " + user.getCarList());
            System.out.println("Invoices List : " + user.getInvoicesList());

            userService.processAddUser(user);

            req.getSession().setAttribute("userlogged", user);
            req.getSession().setAttribute("event", "1");
            req.getSession().setAttribute("message", "You are logged");
            resp.sendRedirect(getServletContext().getContextPath() + "/profile");
        }
    }
}
